<template>
		<view v-if="attachment.length>0">
			<text style="margin-left: 20rpx;font-size: 26rpx;color: #333;">附件</text>

			<view style="margin: 20rpx;">
				<u-album v-if="imglist && imglist.length>0" :urls="imglist" keyName="url" singleSize="150rpx"></u-album>
			</view>

			<view style="margin: 20rpx;display: flex;align-items: center;justify-content: space-between;"
				v-for="(pitem, pindex) in pdflist" :key="pindex" class="pdf-item">
				<text style="font-size: 26rpx;color: #333;">{{pitem.nameFile||pitem.name_file}}</text>
				<text style="color: #1573FF;font-size: 26rpx;white-space: nowrap" @click="viewpdf(pitem)">查看</text>
			</view>

		</view>
</template>

<script>
	export default {
		name:"pic-pdf",
		data() {
			return {
			};
		},
		computed:{
			imglist(){
				return this.attachment.filter(item => {
					if ('1' == item.typeFile || '1'==item.type_file) {
						item.url = this.baseImgUrl() + item.url;
						return item;
					}
				})
			},

			pdflist(){
				return this.attachment.filter(item => {
					if ('0' == item.typeFile || '0'==item.type_file) {
						return item;
					}
				})
			}
		},
		props:{
			attachment:{
				type:Array,
				default(){
					return []
				}
			}
		},
		methods:{
			viewpdf(item) {
        uni.showLoading()
				let that = this

        let fileType = 'pdf'
        if (!uni.$u.test.isEmpty(item.url)) {
          fileType = item.url.split('.')[1]
        }

				uni.downloadFile({
					url: that.baseImgUrl() +item.url,
          filePath:'',
					success(res) {
            uni.hideLoading()

            console.log('下载成功了', res)

						let tempPath = res.tempFilePath
						uni.openDocument({
              showMenu:true,
							filePath: tempPath,
              fileType: fileType,
						})
					},
          fail(e){
            uni.hideLoading()
          }
				})
			},
		}

	}
</script>

<style>

</style>
